
<?php
require_once("common.inc.php");

$pageSize = 5;
$pageIndex = isset($_GET["pageIndex"])?trim($_GET["pageIndex"]):1;
$starRow = ($pageIndex-1) * $pageSize;

$sql="select * from `article`" ;
$where = "";

$queryTitle = getParam("Title");
if($queryTitle){
    $where .= " Title like('%$queryTitle%') ";
}

$queryAuthor = getParam("Author");
if($queryAuthor){
    if($where)
        $where .= "AND";
    $where .= " AuthorName like('%$queryAuthor%') ";
}

$queryMinDate = getParam("minDate");
if($queryMinDate){
    if($where)
        $where .= "AND";
    $where .= " CreateTime >= '$queryMinDate' ";
}
$queryMaxDate = getParam("maxDate");
if($queryMaxDate){
    if($where)
        $where .= "AND";
    $where .= " CreateTime <= '$queryMaxDate' ";
}

if($where) 
    $sql .= " WHERE $where ";

$conn = createDb();
$rows = [];
$sql .= " LIMIT $starRow,$pageSize ";
$rs = $conn->query($sql);
while($row = $rs->fetch_assoc()){
    $rows[]=$row;
}
$rs->close();

$pageInfo = pageable("article",$where,"",$pageIndex,$pageSize);
$rows = $pageInfo["items"];
$pageCount =$pageInfo["pageCount"];
$total = $pageInfo["total"];

/*$total = 0;
$sql = " SELECT count(Id) as t FROM `article` ";
if($where) 
    $sql .= " WHERE $where ";
$rs = $conn->query($sql);
$total = $rs->fetch_assoc()["t"];

$pageCount = ceil($total/$pageSize);
if($pageCount<$pageIndex) $pageIndex = $pageCount;
if($pageIndex<1) $pageIndex = 1;*/


require_once("header.inc.php")
?>

                <span class="btn" id="listBtn">列表</span>
                <span class="btn" id="cardBtn">卡片</span>
                    <div id="panel" class="card">           
                        <div class="table">
                            <div class="thead">
                                <div class="tr">
                                    <div class="th"><span class="title">标题</span></div>
                                    <div class="th">作者</div>
                                    <div class="th">创建时间</div>
                                    <div class="th">操作</div>
                                </div>
                            </div>
                            <div class="tbody" >
                                <?php foreach($rows as $row){?>
                                <div class="tr">
                                    <div class="td Title">
                                    <a href="article_detail4.php?Id=<?=$row["Id"]?>"><?=$row["Title"]?></a>
                                    </div>
                                    <div class="td Author">
                                        <img class="user-face" width="20px" height="20px" src="<?="/faces/".$row["AuthorId"].".jpg"?>"/>
                                        <?=$row["AuthorName"]?>
                                    </div>
                                    <div class="td">
                                        <?=$row["CreateTime"]?><br>
                                        <?=$row["UpdateTime"]?>
                                    </div>
                                    <div class="td">
                                        <a href="article_detail4.php?Id=<?=$row["Id"]?>">详情</a>
                                        <?php if($userId===$row["AuthorId"]) { ?>
                                            <a href="article_modify2.php?Id=<?=$row["Id"]?>">修改</a>
                                            <a href="article_delete.php?Id=<?=$row["Id"]?>" class="delete">删除</a>
                                        <?php } ?>
                                    </div>
                                </div>
                                <?php } ?>
                            </div>
                        </div>
                    </div>
                    <div id="recordInfo"> 
                        共<?=$total?>条记录，<?=$pageCount?>页，当前第<input type="text" value="<?=$pageIndex?>" onblur="jumpTo(this.value)"/>页，
                        <a href="list4.php?<?=pageURL(1)?>">首页</a>< 
                            <?php for($i=1 ; $i<=$pageCount ; $i++){?>
                                <a href="list4.php?<?=pageURL($i)?>"> <?=$i?> </a>                            
                            <?php } ?>
                        > 
                        <a href="list4.php?<?=pageURL($pageCount)?>">尾页</a>
                    </div>
                </div>
                <div id="extras"></div>
            </div>
            <script type="text/javascript">
                var deleteLinks = document.getElementsByClassName("delete");
                for(var i=0,j=deleteLinks.length;i<j;i++){
                    var link =  deleteLinks[i];
                    link.onclick = function(e){
                        e = e || event;
                        var confirmd = confirm("你确定要删除吗？");
                        if(!confirmd){
                            e.preventDefault();
                            return false;
                        }
                    }
                }
            </script>
<?php require_once("footer.inc.php");?>



        
